<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>用户注册页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="{{domain}}/resource/admin/mini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>

    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名称</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required" lay-reqtext="用户名称不能为空" placeholder="用户名称将用于登录"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户昵称</label>
            <div class="layui-input-block">
                <input type="text" name="nickname" lay-verify="required" lay-reqtext="用户昵称不能为空" placeholder="用户昵称将用于展示"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" lay-verify="required" lay-reqtext="用户邮箱不能为空" placeholder="请输入用户邮箱"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户权限</label>
            <div class="layui-input-inline">
                <select name="ulevel" lay-verify="required" id="templates">
                    <option value="">权限列表未加载</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red;">
                    警告:&nbsp;用户操作是危险权限，如非必要，请勿给予本权限！
                </span>

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" lay-reqtext="用户密码不能为空"
                    placeholder="请输入安全系数高的密码" value="" class="layui-input">
            </div>
        </div>
        {{# cverify}}
        <div class="layui-form-item">
            <label class="layui-form-label required">图像验证码</label>
            <div class="layui-input-inline">
                <img src="{{domain}}/admin/api/acode?from={{acfrom}}" id="acode" onclick="recode();" alt="验证码加载错误">
            </div>
        </div>
        {{/ cverify}}
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="saveBtn">注册用户</button>
            </div>
        </div>
    </div>
    <script src="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script>
        function recode() {
            document.getElementById('acode').src = '{{domain}}/admin/api/acode?from={{acfrom}}';
        }

        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            loadTemplate('api');

            function loadTemplate(value) {
                $.get('{{domain}}/admin/api/user/ulevel?do=get', function (res) {
                    if (res.data != '') {
                        // 将数据渲染至下拉框
                        $('#templates').html('');

                        for (var index in res.data) {
                            $('#templates').append('<option value="' + res.data[index].id +
                                '">' +
                                res.data[index].title + '</option>');
                        }
                        form.render('select');
                    }
                });
            }

            //监听提交
            form.on('submit(saveBtn)', function (data) {

                //{{# cverify}}
                layer.prompt({
                    title: '请输入图形验证码:'
                }, function (val, index) {
                    $.get('{{domain}}/admin/api/acode?from={{acfrom}}&check=' + val, function (
                        res) {
                        if (res.msg == '验证码数据不存在') {
                            layer.msg('验证已过期，请刷新验证码后重试...');
                            return false;
                        } else if (res.msg == '验证码输入错误') {
                            layer.msg('验证码输入错误，请重试...');
                            return false;
                        }

                        // 提交新增请求
                        $.post('{{domain}}/admin/api/user/adduser?from=add.html', {
                            username: data.field.username,
                            nickname: data.field.nickname,
                            password: data.field.password,
                            ulevel: data.field.ulevel,
                            email: data.field.email
                        }, function (res) {
                            if (res.msg != '') {
                                layer.msg(res.msg + '...');
                            } else if (res.data == 'OK') {
                                var iframeIndex = parent.layer.getFrameIndex(
                                    window.name);
                                parent.layer.close(iframeIndex);
                                parent.window.location.reload();
                            } else {
                                layer.msg('未知的程序错误...');
                            }
                        }).error(function (res) {
                            layer.msg('操作异常：' + res.responseJSON.msg + '！');
                        });

                    });
                    layer.close(index);
                });
                //{{/ cverify}}

                //{{^ cverify}}
                // 提交新增请求
                $.post('{{domain}}/admin/api/user/adduser?from=add.html', {
                    username: data.field.username,
                    nickname: data.field.nickname,
                    password: data.field.password,
                    ulevel: data.field.ulevel,
                    email: data.field.email
                }, function (res) {
                    if (res.msg != '') {
                        layer.msg(res.msg + '...');
                    } else if (res.data == 'OK') {
                        var iframeIndex = parent.layer.getFrameIndex(
                            window.name);
                        parent.layer.close(iframeIndex);
                        parent.window.location.reload();
                    } else {
                        layer.msg('未知的程序错误...');
                    }
                }).error(function (res) {
                    layer.msg('操作异常：' + res.responseJSON.msg + '！');
                });
                //{{/ cverify}}

                return false;

            });

        });
    </script>
</body>

</html>